<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isdeleteShow">
        <span @click="$emit('clear-search-histories')">全部删除</span>
        &nbsp;&nbsp;
        <span @click="isdeleteShow = false">完成</span>
      </div>
      <van-icon v-else name="delete" @click="isdeleteShow = true"/>
    </van-cell>

    <van-cell
      :title="item"
      v-for="(item, index) in searchHistories"
      :key="index"
      @click="searchItemClick(item,index)"
    >
      <van-icon v-show="isdeleteShow" name="close" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isdeleteShow: false, // 控制删除的显示状态
      searchHistory: this.searchHistories // props的值不能直接修改
    }
  },

  methods: {
    searchItemClick (item, index) {
      if (this.isdeleteShow) {
        // 处于删除状态，执行删除对应的历史记录
        this.searchHistory.splice(index, 1)
      } else {
        // 处于非删除状态，直接进入搜索
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style>
</style>
